package cn.edu.hpu.jie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.edu.hpu.jie.entity.Candidate;
import cn.edu.hpu.jie.entity.CandidateSites;
import cn.edu.hpu.jie.util.RowMapping;
import cn.edu.hpu.jie.util.VoteDB;

public class CandidateDao {
	private VoteDB<Candidate> db = new VoteDB<Candidate>();
	private VoteDB<CandidateSites> dbs = new VoteDB<CandidateSites>();
	
	private class CandidateSitesMapping implements RowMapping<CandidateSites>{

		public CandidateSites getMapping(ResultSet rs) throws SQLException {
			CandidateSites cs = new CandidateSites();
			cs.setContent(rs.getString("content"));
			cs.setId(rs.getInt("id"));
			cs.setSubtitle(rs.getString("subtitle"));
			cs.setTitle(rs.getString("title"));
			return cs;
		}
	}
	
	private class CandidateMapping implements RowMapping<Candidate>{

		public Candidate getMapping(ResultSet rs) throws SQLException {
			Candidate c = new Candidate();
			c.setBirthday(rs.getString("birthday"));
			c.setCollege(rs.getString("college"));
			c.setId(rs.getInt("id"));
			c.setMajor(rs.getString("major"));
			c.setName(rs.getString("name"));
			c.setNation(rs.getString("nation"));
			c.setPolitics(rs.getString("politics"));
			c.setSex(rs.getInt("sex"));
			c.setSites(rs.getString("sites"));
			c.setStudentid(rs.getString("studentid"));
			c.setEnable(rs.getBoolean("enable"));
			return c;
		}
	}
	
	public Candidate getCandidateByStudentId(String studentid) {
		String sql = "SELECT * FROM t_candidate WHERE studentid = ?";
		return db.getObject(sql, new CandidateMapping(), studentid);
	}

	public int insertCandidate(Candidate c, CandidateSites cands) {
		String sql1 = "INSERT INTO t_candidate (birthday, college, major, name, nation, politics, sex, sites, studentid, enable) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		int key = db.getInsertPrimaryKey(sql1, c.getBirthday(), c.getCollege(), c.getMajor(), c.getName(), c.getNation(), c.getPolitics(), c.getSex(), c.getSites(), c.getStudentid(), true);
		
		if(key == 0) {
			return key;
		}
		
		String sql2 = "INSERT INTO t_candidate_sites (id, title, subtitle, content) VALUES (?, ?, ?, ?)";
		int lines = db.updateData(sql2, key, cands.getTitle(), cands.getSubtitle(), cands.getContent());
		if(lines != 0) {
			lines = key;
		}
		return lines;
	}

	public List<Candidate> getCandidateList() {
		String sql = "SELECT * FROM t_candidate ORDER BY studentid";
		return db.getAll(sql, new CandidateMapping());
	}

	public Candidate getCandidteById(int id) {
		String sql = "SELECT * FROM t_candidate WHERE id = ?";
		return db.getObject(sql, new CandidateMapping(), id);
	}

	public CandidateSites getCandidateSitesById(int id) {
		String sql = "SELECT * FROM t_candidate_sites WHERE id = ?";
		return dbs.getObject(sql, new CandidateSitesMapping(), id);
	}

	public int updateCandidateById(Candidate c) {
		String sql = "UPDATE t_candidate SET birthday = ?, college = ?, major = ?, name = ?, nation = ?, politics = ?, sex = ?, sites = ?, studentid = ? WHERE id = ?";
		return db.updateData(sql, c.getBirthday(), c.getCollege(), c.getMajor(), c.getName(), c.getNation(), c.getPolitics(), c.getSex(), c.getSites(), c.getStudentid(), c.getId());
	}

	public int updateCandidateSitesById(CandidateSites cands) {
		String sql = "UPDATE t_candidate_sites SET title = ?, subtitle = ?, content = ? WHERE id = ?";
		return dbs.updateData(sql, cands.getTitle(), cands.getSubtitle(), cands.getContent(), cands.getId());
	}

	public int setEnableById(int id) {
		String sql = "UPDATE t_candidate SET `enable` = !`enable` WHERE id = ?";
		return db.updateData(sql, id);
	} 
}
